/*
 *  AMPIconFrameUpdate.cpp
 *  AMPlugin
 *
 *  Created by Toan on 12/18/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "VCPlugInHeaders.h"
// General includes:
#include "UIDList.h"
#include "Utils.h"
#include "ITextModel.h"
#include "IColorListControlData.h"
#include "ISnipRunParameterUtils.h"
#include "SnpTextModelHelper.h"
#include "IParcel.h"
#include "IHierarchy.h"
#include "IGraphicFrameData.h"
#include "ITOPSplineData.h"
#include "IAnchoredObjectData.h"
#include "IFrameUtils.h"
#include "ILiveEditFacade.h"
#include "IIntData.h"

// General includes:
#include "LocaleSetting.h"
#include "FileTypeRegistry.h"
#include "FileUtils.h"
#include "UIDList.h"
#include "SDKFileHelper.h"
#include "ILayoutUIUtils.h"
// Framework includes:
#include "AMPIconFrameUpdate.h"
#include "IPageItemAdornmentList.h"
#include "IClassIDData.h"
#include "IStoryList.h"
#include "IPageItemTypeUtils.h"
#include "ITLAdornmentUtils.h"
#include "ILockPosition.h"
#include "SnpGraphicHelper.h"
#include "K2Vector.h"
#include "IStoryUtils.h"

#include "StreamUtil.h"
#include "IItemLockData.h"


void AMPIconFrameUpdate::LockPageItem(const UIDRef& pageItem)
{
	
	if (pageItem != kInvalidUIDRef)
	{
		UIDList resultList = UIDList(pageItem.GetDataBase());
		InterfacePtr<IHierarchy> hier(pageItem, IID_IHIERARCHY);
		hier->GetDescendents(&resultList, IID_ILOCKPOSITION);
		
		int32 count = resultList.Length();
		while (count-- > 0)
		{
			InterfacePtr<ILockPosition> lock(resultList.GetRef(count), IID_ILOCKPOSITION);
			if (lock)
				lock->Set(ILockPosition::kLock);
		}
		
	}
}

UIDRef AMPManipulateTextFrame::QueryGraphicFrameRef(UIDRef storyUIDRef)
{
	UIDRef result = UIDRef::gNull;
	do {
		InterfacePtr<ITextModel> textModel(storyUIDRef, UseDefaultIID());
		ASSERT(textModel);
		if (!textModel) {
			break;
		}
		
		// Ask which TextIndex should be selected.
		Utils<ISnipRunParameterUtils> parameterUtils;
		//int32 totalLength = textModel->TotalLength();
		TextIndex at = 0;
		
		InterfacePtr<ITextFrameColumn> textFrameColumn(QueryTextFrameContaining(textModel, at));
		if (!textFrameColumn) {
			break;
		}
		
		UIDRef graphicFrameRef = GetGraphicFrameRef(textFrameColumn, kFalse/*IsTOPFrameAllowed*/);
		if (graphicFrameRef == UIDRef::gNull) {
			break;
		}else {
			result = graphicFrameRef;
		}
		
	}while(kFalse);
	
	return result;
}


ITextFrameColumn* AMPManipulateTextFrame::QueryTextFrameContaining(ITextModel* textModel, const TextIndex at)
{
	InterfacePtr<ITextFrameColumn> resultTFC;
	do {
		SnpTextModelHelper textModelHelper;
		InterfacePtr<IParcel> parcel(textModelHelper.QueryParcelContaining(textModel, at));
		if (!parcel) {
			break;
		}
		resultTFC.reset(parcel->QueryFrame());
	} while(false);
	return resultTFC.forget();
}

UIDRef AMPManipulateTextFrame::GetGraphicFrameRef(const InterfacePtr<ITextFrameColumn>& textFrameColumn, const bool16 isTOPFrameAllowed)
{
	UIDRef result = UIDRef::gNull;
	
	do {
		ASSERT(textFrameColumn);
		if (!textFrameColumn) {
			break;
		}
		
		// Check for a regular text frame by going up
		// the hierarchy till we find a parent object
		// that aggregates IGraphicFrameData. This is
		// the graphic frame that contains the text content.
		UIDRef graphicFrameDataUIDRef = UIDRef::gNull;
		InterfacePtr<IHierarchy> child(textFrameColumn, UseDefaultIID());
		if (child == nil) {
			break;
		}
		do {
			InterfacePtr<IHierarchy> parent(child->QueryParent());
			if (parent == nil) {
				break;
			}
			InterfacePtr<IGraphicFrameData> graphicFrameData(parent, UseDefaultIID());
			if (graphicFrameData != nil) {
				// We have a text frame.
				graphicFrameDataUIDRef = ::GetUIDRef(graphicFrameData);
				break;
			}
			child = parent;
		} while(child != nil);
		
		if (graphicFrameDataUIDRef == UIDRef::gNull) {
			break;
		}
		
		InterfacePtr<ITOPSplineData> topSplineData(graphicFrameDataUIDRef, UseDefaultIID());
		if (topSplineData) {
			// We have a text on a path frame.
			if (isTOPFrameAllowed == kTrue) {
				// Return the text on a path frame
				result = graphicFrameDataUIDRef;
			}
			else {
				// Return the graphic frame associated with the text on a path frame.
				UID mainSplineItemUID = topSplineData->GetMainSplineItemUID();
				ASSERT(mainSplineItemUID != kInvalidUID);
				result = UIDRef(graphicFrameDataUIDRef.GetDataBase(), mainSplineItemUID);
			}
		}
		else {
			// We have a normal graphic frame.
			result = graphicFrameDataUIDRef;
		}
		
	} while(false);
	
	return result;
	
}

/*
#include "ILinkManager.h"
#include "LinkQuery.h"
#include "InCopyImportID.h"
#include "IStoryUtils.h"
bool AMPUtils::QueryLinkImage(IDocument* document)
{
	// +precondition
	ASSERT(document);
	if(!document) {
		//cout << "Cannot load document" << endl;
		return NULL;
	}
	IDataBase* database = ::GetDataBase(document);
	
	InterfacePtr<ILinkManager> linksManager(document, IID_ILINKMANAGER);
	LinkQuery Query;
	ILinkManager::QueryResult QueryResult;
	linksManager->QueryLinks(Query, QueryResult);
	int32 linkCount = QueryResult.size();
	if(linkCount == 0)
	{
		return TRUE;
	}
	//cout << "link count " << linkCount << endl;
	for(int32 link = 0; link < linkCount; link++) 
	{
		// This produces a linkref with a stored UID that makes sense.
		UIDRef linkRef = UIDRef(database, QueryResult[link]);  
		InterfacePtr<ILink> ALink(linkRef, UseDefaultIID());
		InterfacePtr<ILinkResource> resource(linksManager->QueryResourceByUID(ALink->GetResource()));
		//cout << "link count " << linkCount << endl;
		resource->SetState(ILinkResource::kMissing);
		
		//Utils<Facade::ILiveEditFacade>()->CheckOut( linkRef, ICommand::kAutoUndo, nil, kFalse, K2::kSuppressUI );
		/*
		 PMString nPath = resource->GetLongName(FALSE); //gets full path
		 PMString nName = resource->GetShortName(FALSE); //gets full name
		 string pathtmp = nPath.GetPlatformString().c_str();
		 string nametmp = nName.GetPlatformString().c_str();
		 
		 string temp = pathtmp.c_str();
		 temp = temp.substr(0, temp.find_last_of("//"));
		 temp = temp.substr(temp.find_last_of("//")+1);
		 string srcFile = pathtmp.substr(0, pathtmp.find_last_of("/")+1);
		 
		////cout << "srcFile: " << srcFile << endl;
	}
	return TRUE;
}
*/